package com.ruoyi.web.controller.race;
import java.util.List;
import java.util.Arrays;

import cn.hutool.core.collection.CollectionUtil;
import jakarta.servlet.http.HttpServletResponse;

import com.ruoyi.live.base.domain.LiveExamClass;
import com.ruoyi.live.base.service.ILiveExamClassService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.live.base.domain.LiveExamUser;
import com.ruoyi.live.base.service.ILiveExamUserService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 赛场管理Controller
 *
 * @author ruoyi
 * @date 2023-02-25
 */
@RestController
@RequestMapping("/race/arena")
public class LiveExamArenaController extends BaseController
{
    @Autowired
    private ILiveExamUserService liveExamUserService;

    @Autowired
    private ILiveExamClassService liveExamClassService;

    /**
     * 查询竞赛关联的班级
     * @param liveExamClass 查询条件
     * @return 关联班级列表
     */
    @PreAuthorize("@ss.hasPermi('race:user:list')")
    @GetMapping("/relatedClasses")
    public TableDataInfo relatedClasses(LiveExamClass liveExamClass)
    {
        QueryWrapper<LiveExamClass> wrapper = new QueryWrapper<>(liveExamClass);
        List<LiveExamClass> list = liveExamClassService.list(wrapper);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('race:user:add')")
    @PostMapping("/relatedClasses")
    public AjaxResult saveRelatedClass(@RequestBody List<LiveExamClass> list) {
        if(CollectionUtil.isEmpty(list)) {
            return AjaxResult.error();
        }
        return toAjax(liveExamClassService.saveRelatedClass(list));
    }

    /**
     * 查询赛场管理列表
     */
    @PreAuthorize("@ss.hasPermi('race:user:list')")
    @GetMapping("/list")
    public TableDataInfo list(LiveExamUser liveExamUser)
    {
        startPage();
        List<LiveExamUser> list = liveExamUserService.selectExamUsersObj(liveExamUser);
        return getDataTable(list);
    }

    /**
     * 导出赛场管理列表
     */
    @PreAuthorize("@ss.hasPermi('race:user:export')")
    @Log(title = "赛场管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LiveExamUser liveExamUser)
    {
        QueryWrapper<LiveExamUser> wrapper = new QueryWrapper<>(liveExamUser);
        List<LiveExamUser> list = liveExamUserService.list(wrapper);
        ExcelUtil<LiveExamUser> util = new ExcelUtil<LiveExamUser>(LiveExamUser.class);
        util.exportExcel(response, list, "赛场管理数据");
    }

    /**
     * 获取赛场管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('race:user:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(liveExamUserService.getById(id));
    }

    /**
     * 新增赛场管理
     */
    @PreAuthorize("@ss.hasPermi('race:user:add')")
    @Log(title = "赛场管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LiveExamUser liveExamUser)
    {
        return toAjax(liveExamUserService.save(liveExamUser));
    }

    /**
     * 修改赛场管理
     */
    @PreAuthorize("@ss.hasPermi('race:user:edit')")
    @Log(title = "赛场管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LiveExamUser liveExamUser)
    {
        return toAjax(liveExamUserService.updateById(liveExamUser));
    }

    /**
     * 删除赛场管理
     */
    @PreAuthorize("@ss.hasPermi('race:user:remove')")
    @Log(title = "赛场管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        List<Long> list = Arrays.asList(ids);
        return toAjax(liveExamUserService.removeByIds(list));
    }
}
